package com.mentaleye.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.TimeTableBean;
import com.mentaleye.dal.dao.TimeTableDao;
import com.mentaleye.dal.model.TimeTable;
import com.mentaleye.dal.model.TimeTableExample;
import com.mentaleye.dal.model.TimeTableExample.Criteria;
import com.mentaleye.service.TimeTableService;
import com.mentaleye.util.DateUtils;
import com.mentaleye.util.ObjectUtil;

@Service
public class TimeTableServiceImpl implements TimeTableService{

	@Autowired private TimeTableDao timeTableDao;
	
	public boolean add(TimeTableBean timeTableBean){
		TimeTable record = (TimeTable) ObjectUtil.transfer(timeTableBean, TimeTable.class);
		return timeTableDao.insert(record);
	}
	
	public boolean addList(List<TimeTableBean> list){
		@SuppressWarnings("unchecked")
		List<TimeTable> timeTableList = ObjectUtil.transferList(list, TimeTable.class);
		return timeTableDao.insertList(timeTableList);
	}
	
	public boolean removeByTime(Integer curriculumId,String beginTime,String endTime){
		TimeTableExample example = new TimeTableExample();
		Criteria create = example.createCriteria();
		create.andBeginTimeEqualTo(DateUtils.getDateByStr(beginTime, "yyyy-MM-dd HH:mm:ss"));
		create.andEndTimeEqualTo(DateUtils.getDateByStr(endTime, "yyyy-MM-dd HH:mm:ss"));
		create.andCurriculumIdEqualTo(curriculumId);
		return timeTableDao.deleteByExample(example);
	}
	
	public boolean removeById(Integer id){
		return timeTableDao.delete(id);
	}
	
	public boolean removeList(List<Integer> values){
		TimeTableExample example = new TimeTableExample();
		example.createCriteria().andIdIn(values);
		return timeTableDao.deleteByExample(example);
	}

	public boolean removeByClassIdList(List<Integer> oaclassIds){
		return timeTableDao.deleteByList(oaclassIds);
	}
	
	public boolean removeByTimeTableId(Integer timeTableId){
		TimeTableExample example = new TimeTableExample();
		example.createCriteria().andTimeTableIdEqualTo(timeTableId);
		return timeTableDao.deleteByExample(example);
	}
	
	public boolean removeByExample(TimeTableExample example){
		return timeTableDao.deleteByExample(example);
	}
	
	public boolean modify(TimeTableBean timeTableBean){
		TimeTable record = (TimeTable) ObjectUtil.transfer(timeTableBean, TimeTable.class);
		return timeTableDao.update(record);
	}

	public boolean modifyTeacherByMap(Map<String,Object> map){
		return timeTableDao.updateTeacherByMap(map);
	}
	
	public boolean modifyByList(List<TimeTableBean> list){
		@SuppressWarnings("unchecked")
		List<TimeTable> timeTableList = ObjectUtil.transferList(list, TimeTable.class);
		return timeTableDao.updateByList(timeTableList);
	}
	
	public boolean modifyByExample(TimeTableBean timeTableBean,TimeTableExample example){
		TimeTable record = (TimeTable) ObjectUtil.transfer(timeTableBean, TimeTable.class);
		return timeTableDao.updateByExample(record, example);
	}
	
	public TimeTableBean queryById(Integer id){
		return (TimeTableBean) ObjectUtil.transfer(timeTableDao.selectByKey(id), TimeTableBean.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<TimeTableBean> queryByTimeTableId(Integer timeTableId){
		TimeTableExample example = new TimeTableExample();
		example.createCriteria().andTimeTableIdEqualTo(timeTableId);
		return ObjectUtil.transferList(timeTableDao.selectByExample(example), TimeTableBean.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<TimeTableBean> queryByClassId(int oaClassId){
		return ObjectUtil.transferList(timeTableDao.selectByClassId(oaClassId), TimeTableBean.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<TimeTableBean> queryByExample(TimeTableExample example){
		return ObjectUtil.transferList(timeTableDao.selectByExample(example), TimeTableBean.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<TimeTableBean> queryByBeginTime(Integer timeTableId,String beginTime){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("timeTableId", timeTableId);
		map.put("beginTime", beginTime);
		return ObjectUtil.transferList(timeTableDao.selectByBeginTime(map), TimeTableBean.class);
	}
	
}
